﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 职员信息
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.12.01 
    /// </summary>
    public class EmployeeBll
    {
        private EmployeeIService service = new EmployeeService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<EmployeeEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<EmployeeEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<EmployeeEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Delete(EmployeeEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Create(EmployeeEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        public async Task<int> Update(string id, EmployeeEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="dt">数据源</param>
        public void ImportData(DataTable dt)
        {
            try
            {
                List<EmployeeEntity> employeeList = new List<EmployeeEntity>();
                foreach (DataRow dataRow in dt.Rows)
                {
                    EmployeeEntity entity = new EmployeeEntity();
                    entity.Id = CommonHelper.Guid;
                    entity.CreatorTime = DateTime.Now;
                    entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                    if (!dataRow[0].IsNullOrEmpty())
                    {
                        entity.EnCode = dataRow[0].ToString();
                    }
                    if (!dataRow[1].IsNullOrEmpty())
                    {
                        entity.FullName = dataRow[1].ToString();
                    }
                    if (!dataRow[2].IsNullOrEmpty())
                    {
                        entity.Gender = dataRow[2].ToString();
                    }
                    if (!dataRow[3].IsNullOrEmpty())
                    {
                        entity.DepartmentName = dataRow[3].ToString();
                    }
                    if (!dataRow[4].IsNullOrEmpty())
                    {
                        entity.PositionName = dataRow[4].ToString();
                    }
                    if (!dataRow[5].IsNullOrEmpty())
                    {
                        entity.WorkingNature = dataRow[5].ToString();
                    }
                    if (!dataRow[6].IsNullOrEmpty())
                    {
                        entity.IDNumber = dataRow[6].ToString();
                    }
                    if (!dataRow[7].IsNullOrEmpty())
                    {
                        entity.Telephone = dataRow[7].ToString();
                    }
                    if (!dataRow[8].IsNullOrEmpty())
                    {

                        entity.AttendWorkTime = dataRow[8].ToDateOrNull();
                    }
                    if (!dataRow[9].IsNullOrEmpty())
                    {
                        entity.Birthday = dataRow[9].ToDateOrNull();
                    }
                    if (!dataRow[10].IsNullOrEmpty())
                    {
                        entity.Education = dataRow[10].ToString();
                    }
                    if (!dataRow[11].IsNullOrEmpty())
                    {
                        entity.Major = dataRow[11].ToString();
                    }
                    if (!dataRow[12].IsNullOrEmpty())
                    {
                        entity.GraduationAcademy = dataRow[12].ToString();
                    }
                    if (!dataRow[13].IsNullOrEmpty())
                    {
                        entity.GraduationTime = dataRow[13].ToDateOrNull();
                    }
                    LogFactory.GetLogger().Debug("导入数据实体：" + entity.ToJson());
                    employeeList.Add(entity);
                    Thread.Sleep(100);
                }
                var index = service.Create(employeeList).Result;
                LogFactory.GetLogger().Debug("导入数据成功标志：" + index);
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Debug("导入数据错误：" + ex.Message);
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
